/*
 * Hibernate Search, full-text search for your domain model
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */
package org.hibernate.checkstyle.filters;

import java.io.File;

import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.Filter;

/**
 * Allows disabling some rules for the test suite source.
 *
 * Any violation will be suppressed if it's generated by a source file
 * having {@code /src/test/java} included in its path and if the violation
 * message contains the keyword "[not required for tests]".
 * <p>
 * A SuppressionFilter is too generic, and requires per-module configuration.
 *
 * @author Sanne Grinovero
 */
public class ExcludeTestPackages implements Filter {

	private static final String SUB_PATH = File.separator + "src" + File.separator + "test" + File.separator + "java";
	private static final String MESSAGE_DISABLE_KEYWORD = "[not required for tests]";

	@Override
	public boolean accept(AuditEvent aEvent) {
		String fileName = aEvent.getFileName();
		if ( fileName != null && fileName.contains( SUB_PATH ) ) {
			return acceptTestfileEvent( aEvent );
		}
		return true;
	}

	private boolean acceptTestfileEvent(AuditEvent aEvent) {
		String message = aEvent.getMessage();
		if ( message != null && message.contains( MESSAGE_DISABLE_KEYWORD ) ) {
			return false;
		}
		return true;
	}
}
